package com.aios.action.door;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.aios.action.AbstractManagementAction;
import com.aios.entity.Door;
import com.aios.entity.mapper.AssignedState;
import com.aios.exception.ItemNotFoundException;
import com.aios.exception.NullReferenceAccessException;
import com.aios.service.DoorService;

public abstract class AbstractDoorAction extends AbstractManagementAction {

	private static final long serialVersionUID = 1L;

	@Qualifier("doorService")
	@Autowired
	protected DoorService doorService;

	protected Door door;

	protected List<AssignedState> existingUsers;
	protected List<AssignedState> existingGroups;

	protected List<AssignedState> notExistingUsers;
	protected List<AssignedState> notExistingGroups;

	public Door findById() {
		if (id != null) {
			try {
				return doorService.findById(id);
			} catch (Exception e) {
				throw new ItemNotFoundException("Id : " + id);
			}
		} else {
			// logger
			throw new NullReferenceAccessException("Id cannot be null");
		}
	}

	public Door getDoor() {
		return door;
	}

	public void setDoor(Door door) {
		this.door = door;
	}

	public List<AssignedState> getExistingUsers() {
		return existingUsers;
	}

	public void setExistingUsers(List<AssignedState> existingUsers) {
		this.existingUsers = existingUsers;
	}

	public List<AssignedState> getExistingGroups() {
		return existingGroups;
	}

	public void setExistingGroups(List<AssignedState> existingGroups) {
		this.existingGroups = existingGroups;
	}

	public List<AssignedState> getNotExistingUsers() {
		return notExistingUsers;
	}

	public void setNotExistingUsers(List<AssignedState> notExistingUsers) {
		this.notExistingUsers = notExistingUsers;
	}

	public List<AssignedState> getNotExistingGroups() {
		return notExistingGroups;
	}

	public void setNotExistingGroups(List<AssignedState> notExistingGroups) {
		this.notExistingGroups = notExistingGroups;
	}

	public void commonPopulate() {
		notExistingGroups = doorService.findAvailableGroupsByDoorId(getId());
		notExistingUsers = doorService.findAvailableUsersByDoorId(getId());		
	}

	public void inputPopulate() {
		this.commonPopulate();
	}

	public void editPopulate() {
		this.commonPopulate();
		existingGroups = doorService.findAssignedGroupsByDoorId(getId());
		existingUsers = doorService.findAssignedUsersByDoorId(getId());
	}

}
